import java.util.Stack;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: hp
 * Date: 2024-04-19
 * Time: 10:46
 */
public class Sort {
    public static void insertSort(int[] array) {
        for(int i = 1; i < array.length; i++) {
            int tmp = array[i];
            int j = i - 1;
            for(;j >= 0; j--) {
                if(array[j] > tmp) {
                    array[j+1] = array[j];
                }else {
                    break;
                }
            }
            array[j+1] = tmp;
        }
    }


    public static void shellsort(int[] array) {
        int gap = array.length;
        while(gap > 1) {
            gap = gap / 3 + 1;
            shell(array,gap);
        }
    }

    public static void shell(int[] array,int gap) {
        for(int i = gap; i < array.length; i++) {
            int tmp = array[i];
            int j = i-gap;
            for(; j >= 0; j-=gap) {
                if(array[j] > tmp) {
                    array[j+gap] = array[j];
                }else {
                    break;
                }
            }
            array[j+gap] = tmp;
        }
    }


    public static void swap(int[] array,int i,int j) {
        int tmp = array[i];
        array[i] = array[j];
        array[j] = tmp;
    }
    public static void selectSort1(int[] array) {
        for(int i = 0; i < array.length; i++) {
            int minIndix = i;
            for(int j = i+1; j < array.length; j++) {
                if(array[j] < array[minIndix]) {
                    minIndix = j;
                }
            }
            swap(array,i,minIndix);
        }
    }



    public static void selectSort2(int[] array) {
        int left = 0;
        int right = array.length-1;
        while(left < right) {
            int maxIndex = left;
            int minxIndex = left;
            for(int j = left + 1; j <= right; j++) {
                if(array[j] > array[maxIndex]) {
                    maxIndex = j;
                }
                if(array[j] < array[minxIndex]) {
                    minxIndex = j;
                }
            }
            swap(array,minxIndex,left);
            if(maxIndex == left) {
                maxIndex = minxIndex;
            }
            swap(array,maxIndex,right);
            left++;
            right--;
        }
    }


    public static void bubbleSort(int[] array) {
        for (int i = 0; i < array.length - 1; i++) {
            int flg = 1;
            for(int j = 0; j < array.length-1-i; j++) {
                if(array[j] > array[j+1]) {
                    swap(array,j,j+1);
                    flg = 0;
                }
            }
            if(flg == 1) {
                break;
            }
        }
    }



    public static void quickSort(int[] array) {
        quick(array,0,array.length-1);
    }

    private static void quick(int[] array,int start,int end) {
        if(start >= end) {
            return;
        }

        if(end - start + 1 <= 10) {
            insertSortRange(array,start,end);
            return;
        }

        //三数取中法
        //1 2 3 4 5
        int Index = midThree(array,start,end);
        swap(array,start,Index);
        //3 1 2 4 5

        int par = patition(array,start,end);

        quick(array,start,par-1);
        quick(array,par+1,end);
    }
    public static void insertSortRange(int[] array,int left,int right) {
        for(int i = left + 1; i < right; i++) {
            int tmp = array[i];
            int j = i - 1;
            for(;j >= left; j--) {
                if(array[j] > tmp) {
                    array[j+1] = array[j];
                }else {
                    break;
                }
            }
            array[j+1] = tmp;
        }
    }

    private static int patitionHoare(int[] array,int left,int right) {
        int i = left;
        int tmp = array[left];
        while(left < right) {
            while(left < right && array[right] >= tmp) {
                right--;
            }
            while(left < right && array[left] <= tmp) {
                left++;
            }
            swap(array,left,right);
        }
        swap(array,left,i);
        return left;
    }

    private static int patition(int[] array,int left,int right) {
        int tmp = array[left];
        while(left < right) {
            if(left < right && array[right] >= tmp) {
                right--;
            }
            array[left] = array[right];
            if(left < right && array[left] <= tmp) {
                left++;
            }
            array[right] = array[left];
        }
        array[left] = tmp;
        return left;
    }

    public static int midThree(int[] array,int left,int right) {
        int mid = (left+right) / 2;
        if(array[left] < array[right]) {
            if(array[left] > array[mid]) {
                return left;
            }else if(array[right] < array[mid]) {
                return right;
            }else {
                return mid;
            }
        }else {
            if(array[left] < array[mid]) {
                return left;
            }else if(array[right] > array[mid]) {
                return right;
            }else {
                return mid;
            }
        }
    }

    public static void quickSortNot(int[] array) {
        int left = 0;
        int right = array.length - 1;
        int par = patition(array,left,right);
        Stack<Integer> stack = new Stack<>();
        if(par > left+1) {
            stack.push(left);
            stack.push(par-1);
        }
        if(par < right - 1) {
            stack.push(par+1);
            stack.push(right);
        }

        while(!stack.isEmpty()) {
            right = stack.pop();
            left = stack.pop();
            par = patition(array,left,right);
            if(par > left+1) {
                stack.push(left);
                stack.push(par-1);
            }
            if(par < right - 1) {
                stack.push(par+1);
                stack.push(right);
            }
        }

    }

    public static void mergeSort(int[] array) {
        mergeSortfun(array,0,array.length-1);
    }

    public static void mergeSortfun(int[] array,int left,int right) {
        if(left == right) {
            return;
        }
        int mid = (left + right) / 2;

        mergeSortfun(array,left,mid);
        mergeSortfun(array,mid+1,right);
        merge(array,left,right,mid);
    }

    public static void merge(int[] array,int left,int right,int mid) {
        int s1 = left;
        int e1 = mid;
        int s2 = mid+1;
        int e2 = right;
        int k = 0;
        int[] tmp = new int[right-left+1];
        while(s1 <= e1 && s2 <= e2) {
            if(array[s1] >= array[s2]) {
                tmp[k++] = array[s2++];
            } else {
                tmp[k++] = array[s1++];
            }
        }
        while(s1 <= e1) {
            tmp[k++] = array[s1++];
        }
        while(s2 <= e2) {
            tmp[k++] = array[s2++];
        }

        for(int i = 0; i < k; i++) {
            array[left + i] = tmp[i];
        }
    }

    public static void mergeSortNot(int[] array) {
        int gap = 1;
        while(gap < array.length) {
            for(int i = 0; i < array.length; i=i+gap*2) {
                int left = i;
                int mid = left + gap - 1;
                if(mid >= array.length) {
                    mid = array.length-1;
                }
                int right = mid + gap;
                if(right >= array.length) {
                    right = array.length-1;
                }
                merge(array,left,right,mid);
            }
            gap = gap * 2;
        }
    }

    public static void countSort(int[] array) {
        int max = array[0];
        int min = array[0];
        //找到最小值和最大值
        for(int i = 0; i < array.length; i++) {
            if(array[i] > max) {
                max = array[i];
            }
            if(array[i] < min) {
                min = array[i];
            }
        }
        //通过最小值和最大值来创建数组
        int[] count = new int[max-min+1];
        //遍历数组来计数
        for(int i = 0; i < array.length; i++) {
            count[array[i]-min]++;
        }
        //在原数组重新排序
        int k = 0;
        for (int i = 0; i < count.length; i++) {
            while(count[i] > 0) {
                array[k++] = i + min;
                count[i]--;
            }
        }

    }


}


